//service.js

app.factory('R', ["$http", "$q", 'toaster', function($http, $q, toaster){
    var _trans = function(method, url, data, still){
        var deferred = $q.defer();

        var obj = {
            method: method,
            url: 'http://'+app.server.host+url
        };

        if (method == 'GET') obj.params = data;
        if (method == 'POST') obj.data = {
            'data': data
        };
        
        $http(obj).success(function(data){
            if (data.rtnCode == 0000000 || still){
                deferred.resolve(data);
            }else{
                toaster.error(data.msg);
            }
        }).error(function(){
            toaster.error('网络出问题了，请联系管理员');
        });
        return deferred.promise;
    }
    return {
        getFileUpload: function(){
            return 'http://'+app.server.host+'/ar/common/uploadFile.do';
        },
        getTime: function(str){
            var a = new Date(str);
            return a.getTime();
        },
        getTimeFormat: function(date, fmt){
            var date = new Date(date);
            var o = {
                "M+": date.getMonth() + 1, //月份 
                "d+": date.getDate(), //日 
                "h+": date.getHours(), //小时 
                "m+": date.getMinutes(), //分 
                "s+": date.getSeconds(), //秒 
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
                "S": date.getMilliseconds() //毫秒 
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        },
        login : function(data){
            var url = "/ar/admin/user/login.do", method = "POST";
            return _trans(method, url, data, 1);
        },
        getDemandsByCondition : function(data){
            var url = "/ar/admin/demand/getDemandsByCondition.do", method = "GET";
            return _trans(method, url, data);
        },
        getDemandDetailById : function(data){
            var url = "/ar/common/getDemandDetailById.do", method = "GET";
            return _trans(method, url, data);
        },
        addDemand : function(data){
            var url = "/ar/admin/demand/addDemand.do", method = "POST";
            return _trans(method, url, data);
        },
        updateDemand : function(data){
            var url = "/ar/admin/demand/updateDemand.do", method = "POST";
            return _trans(method, url, data);
        },
        deleteDemand : function(data){
            var url = "/ar/admin/demand/deleteDemand.do", method = "POST";
            return _trans(method, url, data);
        },
        batchUpdateDemandState : function(data){
            var url = "/ar/admin/demand/batchUpdateDemandState.do", method = "POST";
            return _trans(method, url, data);
        },
        getProductsByCondition : function(data){
            var url = "/ar/admin/product/getProductsByCondition.do", method = "GET";
            return _trans(method, url, data);
        },
        getProductDetailById : function(data){
            var url = "/ar/common/getProductDetailById.do", method = "GET";
            return _trans(method, url, data);
        },
        getDepositByCondition : function(data){
            var url = "/ar/admin/demand/getDepositByCondition.do", method = "GET";
            return _trans(method, url, data);
        },
        refundOrder : function(data){
            var url = "/ar/alipay/refundOrder.do", method = "POST";
            return _trans(method, url, data);
        },
        updateDepositPercent : function(data){
            var url = "/ar/admin/demand/updateDepositPercent.do", method = "POST";
            return _trans(method, url, data);
        },
        getDepositPercent : function(data){
            var url = "/ar/admin/demand/getDepositPercent.do", method = "GET";
            return _trans(method, url, data);
        },
        addProduct : function(data){
            var url = "/ar/admin/product/addProduct.do", method = "POST";
            return _trans(method, url, data);
        },
        updateProduct : function(data){
            var url = "/ar/admin/product/updateProduct.do", method = "POST";
            return _trans(method, url, data);
        },
        batchUpdateProductState : function(data){
            var url = "/ar/admin/product/batchUpdateProductState.do", method = "POST";
            return _trans(method, url, data);
        },
        getUsersByCondition : function(data){
            var url = "/ar/admin/user/getUsersByCondition.do", method = "GET";
            return _trans(method, url, data);
        },
        getUserDetailById : function(data){
            var url = "/ar/common/getUserDetailById.do", method = "GET";
            return _trans(method, url, data);
        },
        addUser : function(data){
            var url = "/ar/admin/user/addUser.do", method = "POST";
            return _trans(method, url, data);
        },
        updateUser : function(data){
            var url = "/ar/admin/user/updateUser.do", method = "POST";
            return _trans(method, url, data);
        },
        deleteUser : function(data){
            var url = "/ar/admin/user/deleteUser.do", method = "POST";
            return _trans(method, url, data);
        },
        getTagsByCondition : function(data){
            var url = "/ar/admin/tag/getTagsByCondition.do", method = "GET";
            return _trans(method, url, data);
        },
        addTag : function(data){
            var url = "/ar/admin/tag/addTag.do", method = "POST";
            return _trans(method, url, data);
        },
        saveTag : function(data){
            var url = "/ar/common/saveTag.do", method = "POST";
            return _trans(method, url, data);
        },
        updateTag : function(data){
            var url = "/ar/admin/tag/updateTag.do", method = "POST";
            return _trans(method, url, data);
        },
        deleteTag : function(data){
            var url = "/ar/admin/tag/deleteTag.do", method = "POST";
            return _trans(method, url, data);
        },
        getAllBanner : function(data){
            var url = "/ar/admin/banner/getBannerSurvey.do", method = "GET";
            return _trans(method, url, data);
        },
        getAllBannerByType : function(data){
            var url = "/ar/admin/banner/getAllBanner.do", method = "GET";
            return _trans(method, url, data);
        },
        addBanner : function(data){
            var url = "/ar/admin/banner/addBanner.do", method = "POST";
            return _trans(method, url, data);
        },


        getAllClassifyAndDirection : function(data){
            var url = "/ar/common/getAllClassifyAndDirection.do", method = "GET";
            return _trans(method, url, data);
        },
        getAddressList : function(data){
            var url = "/ar/common/getAddressList.do", method = "GET";
            return _trans(method, url, data);
        },
        getRecomandIndex : function(data){
            var url = "/ar/common/getRecomandIndex.do", method = "GET";
            return _trans(method, url, data);
        },
        getRecomandTags : function(data){
            var url = "/ar/common/getRecomandTags.do", method = "GET";
            return _trans(method, url, data);
        },
        getAllClassify : function(data){
            var url = "/ar/common/getAllClassify.do", method = "GET";
            return _trans(method, url, data);
        }
    }
}]);